import isEmpty from 'lodash/isEmpty';
import { message } from 'antd';

export default function({ namespace, queryService, initService }) {
    return {
        namespace,
        state: {
            searchModel: {
                employeeCode: null,
                period: null
            },
            columns: [],
            dataSource: [],
            rowKey: 'id',
            pagination: {
                current: 1,
                pageSize: 10
            },
            pageLoading: false,
            // 显示弹出对话框
            showImportDialog: false,
            goToLog: () => ({})
        },
        reducers: {
            setMultiStore(state, action) {
                return {
                    ...state,
                    ...action.payload
                };
            },

            mergeSearchModel(state, action) {
                const { searchModel } = state;

                return {
                    ...state,
                    searchModel: { ...searchModel, ...action.payload }
                };
            },

            changePeriod(state, action) {
                const { searchModel } = state;
                const { payload: period } = action;

                return {
                    ...state,
                    searchModel: { ...searchModel, period }
                };
            },

            setPageInfo(state, action) {
                const { pageSize, total, pageNo: current, rows: dataSource } = action.payload.data;

                const pagination = {
                    ...state.pagination,
                    pageSize: pageSize || 10,
                    current: current || 1,
                    total
                };

                return {
                    ...state,
                    dataSource,
                    pagination
                };
            },

            setPagination(state, action) {
                const pagination = {
                    ...state.pagination,
                    current: action.payload.current || state.pagination.current,
                    pageSize: action.payload.pageSize || state.pagination.pageSize
                };

                return {
                    ...state,
                    pagination
                };
            },

            showImportDialog(state, action) {
                return {
                    ...state,
                    showImportDialog: action.payload
                };
            }
        },
        effects: {
            *init(state, { put, call, select }) {
                if (state?.payload?.needReset) {
                    yield put({
                        type: 'setMultiStore',
                        payload: {
                            searchModel: {
                                employeeCode: null,
                                period: null
                            }
                        }
                    });
                }

                const searchModel = yield select(s => s[namespace].searchModel);

                // 查询导入最近导入月份
                const response = yield call(initService, state?.payload?.organizationCode);
                if (!isEmpty(response.data)) {
                    const { year, month } = response.data;

                    // 设置默认查询月份
                    yield put({
                        type: 'setMultiStore',
                        payload: {
                            searchModel: {
                                ...searchModel,
                                organizationCode: state?.payload?.organizationCode,
                                period: `${year}-${(month < 10 ? '0' : '') + month}`
                            }
                        }
                    });

                    // 查询
                    yield put({ type: 'search' });
                } else {
                    yield put({
                        type: 'setMultiStore',
                        payload: {
                            searchModel: {
                                ...searchModel,
                                organizationCode: state?.payload?.organizationCode,
                                period: null
                            },
                            dataSource: []
                        }
                    });
                }
            },

            *search(s, { put, call, select }) {
                const searchCondition = yield select(state => {
                    const currentState = state[namespace];
                    return {
                        searchModel: currentState.searchModel,
                        pagination: currentState.pagination || {
                            current: 1,
                            pageSize: 10
                        }
                    };
                });
                const { searchModel, pagination } = searchCondition;
                const { period, ...otherModel } = searchModel;

                const periodStr = searchModel.period || '';
                const year = periodStr.substr(0, 4);
                const month = periodStr.substr(5);

                const postData = {
                    year,
                    // eslint-disable-next-line radix
                    month: month ? String(parseInt(month)) : month,
                    ...otherModel,
                    page: {
                        pageNo: s?.searchFormPagination ? pagination.current : 1,
                        pageSize: pagination.pageSize
                    }
                };
                const response = yield call(queryService, postData);
                const { code, msg } = response;
                if (code !== 10000) {
                    message.error(msg);
                }
                yield put({
                    type: 'setPageInfo',
                    payload: response
                });
            },

            *tablePageChange(state, { put }) {
                yield put({
                    type: 'setPagination',
                    payload: state.payload
                });
                yield put({ type: 'search', searchFormPagination: true });
            }
        }
    };
}
